package com.base.common.db.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.base.common.common.Constant;
import com.base.common.util.AppTools;
import com.base.common.util.Base64Utils;
import com.base.common.util.StringUtils;

/**
 * 缓存数据
 */
public class MachineCacheInfo {
    Context context;
    private SharedPreferences preferences;

    //机器配置版本号
    private String cfgVersion;
    // App版本号
    private String appVersion;
    //机器串号
    private String mcode;
    //文件路径
    private String adPath = "";
    //心跳间隔(分)
    private Integer minutes;
    //创建时间
    private String createTime;
    //修改时间
    private String modifyTime;
    //修改人
    private String modifier;
    //安全码 (用于打开配置页面的安全认证码)
    private String safeCode;
    //请求的服务器地址
    private String serverUrl;

    public Integer getMinutes() {
        return minutes;
    }

    public void setMinutes(Integer minutes) {
        this.minutes = minutes;
    }

    public String getCfgVersion() {
        return cfgVersion;
    }

    public void setCfgVersion(String cfgVersion) {
        this.cfgVersion = cfgVersion;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    public String getMcode() {
        return mcode;
    }

    public void setMcode(String mcode) {
        this.mcode = mcode;
    }

    public String getAdPath() {
        return adPath;
    }

    public void setAdPath(String adPath) {
        this.adPath = adPath;
    }


    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(String modifyTime) {
        this.modifyTime = modifyTime;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public String getSafeCode() {
        return safeCode;
    }

    public void setSafeCode(String safeCode) {
        this.safeCode = safeCode;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    private static MachineCacheInfo mInstance;

    private MachineCacheInfo(Context context) {
        this.context = context;
        preferences = context.getSharedPreferences(Constant.PrefMachine.MACHINE_CACHE_INFO,
                Context.MODE_PRIVATE);
        getCache();
    }

    public static void init(Context context) {
        if (mInstance == null) {
            mInstance = new MachineCacheInfo(context);
        }
    }

    public static MachineCacheInfo getInstance() {
        return mInstance;
    }

    public static MachineCacheInfo getInstance(Context context) {
        init(context);
        return mInstance;
    }



    public void writeToCache() {
        SharedPreferences.Editor editor = preferences.edit();
        if(StringUtils.isEmpty(mcode)){
            String code = AppTools.getDeviceId(context);
            code = Base64Utils.getBase64(code);
            mcode = code.replace("=", "").toUpperCase();
        }
        editor.putString(Constant.PrefMachine.cfgVersion, cfgVersion);
        editor.putString(Constant.PrefMachine.appVersion, appVersion);
        editor.putString(Constant.PrefMachine.mcode, mcode);
        editor.putString(Constant.PrefMachine.createTime, createTime);
        editor.putString(Constant.PrefMachine.adPath, adPath);
        editor.putInt(Constant.PrefMachine.minutes, minutes);
        editor.putString(Constant.PrefMachine.safeCode, safeCode);
        editor.putString(Constant.PrefMachine.serverUrl, serverUrl);

        editor.commit();
    }

    public void clearCache() {

        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();

    }

    /*数据有刷新需要调用，不然数据不会刷新*/
    public void getCache() {
        cfgVersion = preferences.getString(Constant.PrefMachine.cfgVersion, "");
        appVersion = preferences.getString(Constant.PrefMachine.appVersion, "");
        mcode = preferences.getString(Constant.PrefMachine.mcode, "");
        createTime = preferences.getString(Constant.PrefMachine.createTime, "");
        adPath = preferences.getString(Constant.PrefMachine.adPath, "");
        minutes = preferences.getInt(Constant.PrefMachine.minutes, 5);
        safeCode = preferences.getString(Constant.PrefMachine.safeCode, "");
        serverUrl = preferences.getString(Constant.PrefMachine.serverUrl, "");
    }


}